import 'package:flutter/material.dart';

import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../info_page.dart';

class ChangePasswordPage extends StatefulWidget {
  const ChangePasswordPage({Key? key, required this.onChangePassword})
      : super(key: key);
  static const routeName = '${InfoPage.routeName}/change_password';

  @override
  ChangePasswordPageState createState() {
    return ChangePasswordPageState();
  }

  final void Function(String oldPass, String newPass) onChangePassword;
}

class ChangePasswordPageState extends State<ChangePasswordPage> {
  final _formKey = GlobalKey<FormState>();
  String oldPassword = '';
  String password = '';
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(I18n.change_password),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          children: [
            TextFormField(
              keyboardType: TextInputType.visiblePassword,
              obscureText: true,
              decoration: const InputDecoration(
                border: UnderlineInputBorder(),
                labelText: I18n.old_password,
              ),
              validator: (value) {
                if (value == null ||
                    value.isEmpty ||
                    !RegExp(I18n.password_regex).hasMatch(value)) {
                  return I18n.invalid_password;
                }
                oldPassword = value;
                return null;
              },
            ),
            TextFormField(
              keyboardType: TextInputType.visiblePassword,
              obscureText: true,
              decoration: const InputDecoration(
                border: UnderlineInputBorder(),
                labelText: I18n.new_password,
              ),
              validator: (value) {
                if (value == null ||
                    value.isEmpty ||
                    !RegExp(I18n.password_regex).hasMatch(value)) {
                  return I18n.invalid_password;
                }
                password = value;
                return null;
              },
            ),
            TextFormField(
              keyboardType: TextInputType.visiblePassword,
              obscureText: true,
              decoration: const InputDecoration(
                border: UnderlineInputBorder(),
                labelText: I18n.confirm_password,
              ),
              validator: (value) {
                if (value == null ||
                    value.isEmpty ||
                    !RegExp(I18n.password_regex).hasMatch(value)) {
                  if (value != password) {
                    return I18n.invalid_confirm_password;
                  } else {
                    return I18n.invalid_password;
                  }
                }
                return null;
              },
            ),
            ElevatedButton(
              onPressed: () {
                if (_formKey.currentState!.validate()) {
                  widget.onChangePassword(oldPassword, password);
                }
              },
              child: const Text(I18n.submit),
            ),
            Expanded(
                child: Icon(
              Icons.key,
              color: Colors.grey.shade300,
              size: 240,
            ))
          ],
        ),
      ),
    );
  }
}
